package com.hgw.Demo01_proxyPattern.dynamicProxy_IH;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * Data time:2021/12/13 18:20
 * StudentID:2019112118
 * Author:hgw
 * Description: 客户端测试类
 */
public class Client {
    public static void main(String[] args) {
        InvocationHandler handler = null;
        AbstractUserDAO userDAO = new UserDAO();
        handler = new DAOLogHandler(userDAO);
        AbstractUserDAO proxy = null;

        // 动态创建代理对应，用于代理一个AbstractUserDAO类型的真实主题对象
        proxy = (AbstractUserDAO) Proxy.newProxyInstance(AbstractUserDAO.class.getClassLoader(),new Class[]{AbstractUserDAO.class
        } , handler);
        proxy.findUserById("张无忌");  // 调用代理对象的业务方法

        System.out.println("---------------------------------------");

        AbstractDocumentDAO docDAO = new DocumentDAO();
        handler = new DAOLogHandler(docDAO);
        AbstractDocumentDAO proxy_new = null;

        // 动态创建代理对象，用于代理一个AbstractDocumentDAO类型的真实主题对象
        proxy_new = (AbstractDocumentDAO) Proxy.newProxyInstance(AbstractUserDAO.class.getClassLoader(), new Class[]{AbstractDocumentDAO.class},handler );
        proxy_new.deleteDocumentById("D002");   // 调用代理对象的业务方法
    }
}
